import com.alibaba.moco.RedisPoolInstance
import com.alibaba.moco.util.BaseTypeUtil
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.apache.commons.lang.StringUtils
import redis.clients.jedis.Jedis
import script.GlobalValues
import script.moco.dto.OrderPriceDetail
import script.v1.didiCallback.NoticeCallback
import script.v1.didiCallback.NotifyRequest
/**
 * 模拟 滴滴 司机评分
 * Created by wb-lj240895 on 2016/11/1.
 */
def excute(request) {
    def slurper = new JsonSlurper()
    def params = request.getParameterMap();
    Jedis redis = RedisPoolInstance.getRedisClient()
    //欢行异常判断
    def e = redis.hget(GlobalValues.KEY_EXCEPTION,"cancelOrder")
    if (!StringUtils.isBlank(e)) {
        def eJson = slurper.parseText(e)
        return eJson
    }
    CancelRequest req = BaseTypeUtil.mapToBean(params, CancelRequest.class) as CancelRequest
    String token = req.getAccess_token()
    System.out.println(token)
    CancelResponse response = new CancelResponse()
    if (token != null/* && redis.exists(token)*/) {
        //redis.del(token)
        //判断订单是否存在 存在则更新订单信息
        def orderId = req.getOrder_id()
        if (orderId != null && redis.exists(GlobalValues.KEY_ORDER+orderId)) {
            //redis.del(orderId)
            def detailStr = redis.get(GlobalValues.KEY_ORDER+orderId) /*redis.get(orderId)*/
            def detailJson = slurper.parseText(detailStr)
            def status = detailJson.order.status
            System.out.println(status)
            if (status == 300) {
                detailJson.order.status = 610
                detailJson.order.sub_status = 6101
                redis.set(GlobalValues.KEY_ORDER+ orderId,JsonOutput.toJson(detailJson).toString())
                redis.expire(GlobalValues.KEY_ORDER+ orderId,GlobalValues.RIDES_EXPIRE)
                //发起回调
                //通知回调
                NoticeCallback noticeCallbackTool = new NoticeCallback();
                NotifyRequest notifyRequest = new NotifyRequest();
                notifyRequest.setOrderId(orderId);
                notifyRequest.setNotifyType(2);
                notifyRequest.setNotifyDesc("取消订单无需支付");
                String url = noticeCallbackTool.getCallbackUrlFromRequest(request, false)
                if (com.alibaba.alimonitor.jmonitor.utils.StringUtils.isNotBlank(url)) {
                    noticeCallbackTool.handNotify(url, notifyRequest);
                }

                response.setErrno(0)
                response.setErrmsg("SUCCESS")
                return JsonOutput.toJson(response)
            }else if (status == 400 || status == 410){
                System.out.println(req.getForce())
                if ("false".equals(req.getForce())){
                	def cost = [:]
                    cost.put("cost",0)
                    response.setData(cost)
                    response.setErrno(0)
                    response.setErrmsg("SUCCESS")
                    return JsonOutput.toJson(response)
                }else if ("true".equals(req.getForce())){
                    // 添加费用详情
                    OrderPriceDetail det = new OrderPriceDetail();
                    System.out.println("设置费用详情")
                    det.setAmount("5.00")
                    det.setName("违约费")
                    det.setType("cancel_fee")
                    System.out.println(detailJson.price.detail)

                    detailJson.price.detail.add(det);
                    //redis.set(req.getOrder_id(), JsonOutput.toJson(detailJson).toString())
                    detailJson.order.status = 610
                    detailJson.order.sub_status = 6100
                    redis.set(GlobalValues.KEY_ORDER+ req.getOrder_id(), JsonOutput.toJson(detailJson).toString())
                    redis.expire(GlobalValues.KEY_ORDER+ req.getOrder_id(),GlobalValues.RIDES_EXPIRE)
                    //发起回调
                    //通知回调
                    NoticeCallback noticeCallbackTool = new NoticeCallback();
                    NotifyRequest notifyRequest = new NotifyRequest();
                    notifyRequest.setOrderId(orderId);
                    notifyRequest.setNotifyType(3);
                    notifyRequest.setNotifyDesc("订单取消费用待支付（确认支付）");
                    String url = noticeCallbackTool.getCallbackUrlFromRequest(request, false)
                    if (com.alibaba.alimonitor.jmonitor.utils.StringUtils.isNotBlank(url)) {
                        noticeCallbackTool.handNotify(url, notifyRequest);
                    }
                    response.setErrno(0)
                    def cost = [:]
                    cost.put("cost",5.00)
                    response.setData(cost)
                    response.setErrmsg("SUCCESS")
                    return JsonOutput.toJson(response)
                }

            }
           // redis.set(orderId,detailJson.toString())
            redis.set(GlobalValues.KEY_ORDER+ orderId,JsonOutput.toJson(detailJson).toString())
            redis.expire(GlobalValues.KEY_ORDER+ orderId,GlobalValues.RIDES_EXPIRE)
			def cost = [:]
            cost.put("cost",0)
            response.setData(cost)
            response.setErrno(20013)
            response.setErrmsg("订单已进入行程中无法取消,如有问题请联系客服")
            return JsonOutput.toJson(response)
        }else {
            response.setErrno(-2)
            response.setErrmsg("訂單不存在")
            return JsonOutput.toJson(response)
        }
    } else {
        response.setErrno(-1)
        response.setErrmsg("token错误")
        return JsonOutput.toJson(response)
    }
}

public class CancelRequest {
    String client_id	//	string	yes	申请应用时分配的AppKey
    String access_token	//	string	yes	授权后的access token
    String timestamp	//	int	yes	当前时间戳
    String sign	//	string	yes	签名 详细算法参见 签名验证 章节
    String order_id	//	string	yes	订单id
    String force	//	enum	no	是否强制取消(true 或 false)默认false
}

public class CancelResponse {
    int errno
    String errmsg
    def data
}
